package com.kj.tms.common.utils.calx.formula;


import com.kj.tms.common.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * @ProjectName: ledger
 * @Package: com.hongtu.tms.ledger.calx.factory.formula
 * @ClassName: Logical
 * @Description: [excel 逻辑运算函数]
 * @Author: [zhuyongjie]
 * @CreateDate: 2018/4/17 9:40
 * @UpdateUser: []
 * @UpdateDate: 2018/4/17 9:40
 * @UpdateRemark: []
 * @Version: 1.0
 * Copyright: 北京宏图天安 Copyright (c) 2018
 */
public class Logical {
    private static Logger logger = LoggerFactory.getLogger(Logical.class);

    /**
     * @Method      IF
     * @Param		test
     * @Param		then_value
     * @Param		otherwise_value
     * @Return      java.lang.Object
     * @Exception
     * @Description [IF 逻辑判断]
     * @Author      zhuyongjie
     * @Version     1.0
     * @Date        2018/4/17 17:33
     */
    public static Object IF(String test, String then_value, String otherwise_value) {
        Object result = null;
        if(!StringUtil.isEmptyOrNull(test)){
            boolean parseBoolean = Boolean.parseBoolean(test);
            if(parseBoolean){
                result = StringUtil.isEmptyOrNull(then_value) ? parseBoolean : then_value;
            }else {
                result = StringUtil.isEmptyOrNull(otherwise_value) ? parseBoolean : otherwise_value;
            }
        }
//        System.out.println("Logical.IF("+test+","+then_value+","+otherwise_value+") calculation result : " + result);
        logger.info("Logical.IF("+test+","+then_value+","+otherwise_value+") calculation result : " + result);
        return result;
    }//IF end

    /**
     * @Method      NOT
     * @Param		logical
     * @Return      boolean
     * @Exception
     * @Description [excel not运算]
     * @Author      zhuyongjie
     * @Version     1.0
     * @Date        2018/4/17 18:06
     */
    public static boolean NOT(boolean logical) {
        return !logical;
    }//NOT end

    /**
     * @Method      OR
     * @Param		orList
     * @Return      boolean
     * @Exception
     * @Description [excel or函数]
     * @Author      zhuyongjie
     * @Version     1.0
     * @Date        2018/4/17 18:15
     */
    public static boolean OR(List orList) {
        boolean result = false;
        if(!StringUtil.isEmptyOrNull(orList)){
            for(int i = 0;i < orList.size();i ++) {
                Object orObj = orList.get(i);
                if(orObj instanceof Boolean){
                    boolean v1 = (boolean) orObj;
                    if(v1){
                        result = true;
                        break;
                    }//if end
                }else if(orObj instanceof String){
                    String v1 = StringUtil.toString(orObj).toUpperCase();
                    if(v1.equals("TRUE") || v1.equals("FALSE")){
                        boolean v2 = Boolean.parseBoolean(v1);
                        if(v2){
                            result = true;
                            break;
                        }//if end
                    }//if end
                }//if end
            }//for end
        }
//        System.out.println("Logical.OR("+orList.toString()+") calculation result : " + result);
        logger.info("Logical.OR("+orList.toString()+") calculation result : " + result);
        return result;
    }//OR end

    /**
     * @Method      TRUE
     * @Param
     * @Return      boolean
     * @Exception
     * @Description [excel TRUE()函数]
     * @Author      zhuyongjie
     * @Version     1.0
     * @Date        2018/4/17 18:29
     */
    public static boolean TRUE() {
        return true;
    }

    /**
     * @Method      FALSE
     * @Param
     * @Return      boolean
     * @Exception
     * @Description [excel FALSE()函数]
     * @Author      zhuyongjie
     * @Version     1.0
     * @Date        2018/4/17 18:30
     */
    public static boolean FALSE(){
        return false;
    }

    /**
     * @Method      AND
     * @Param		andList
     * @Return      java.lang.Object
     * @Exception
     * @Description [and()函数]
     * @Author      zhuyongjie
     * @Version     1.0
     * @Date        2018/4/18 9:16
     */
    public static Object AND(List andList) {
        boolean result = true;
        if(!StringUtil.isEmptyOrNull(andList)) {
            for (int i = 0; i < andList.size(); i++) {
                Object orObj = andList.get(i);
                if(orObj instanceof Boolean){
                    boolean v1 = (boolean) orObj;
                    if(!v1){
                        result = false;
                        break;
                    }//if end
                }else if(orObj instanceof String){
                    String v1 = StringUtil.toString(orObj).toUpperCase();
                    if(v1.equals("TRUE") || v1.equals("FALSE")){
                        boolean v2 = Boolean.parseBoolean(v1);
                        if(!v2){
                            result = false;
                            break;
                        }//if end
                    }//if end
                }//if end
            }//for end
        }//if end
//        System.out.println("Logical.AND("+andList.toString()+") calculation result : " + result);
        logger.info("Logical.AND("+andList.toString()+") calculation result : " + result);
        return result;
    }//AND end
}